package org.duanyu.shopping_goods_service.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.duanyu.shopping_common.pojo.Brand;
import org.duanyu.shopping_common.service.BrandService;
import org.duanyu.shopping_goods_service.mapper.BrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public Brand findBrandById(Long id) {
        return brandMapper.selectById(id);
    }

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectList(null);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void delete(Long bid) {
        brandMapper.deleteById(bid);
    }

    @Override
    public Page<Brand> search(Brand brand, int page, int size) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        //判断品牌名是否为空
        if (brand != null&& StringUtils.hasText(brand.getName())) {
            wrapper.like("name", brand.getName());
        }
        return brandMapper.selectPage(new Page<>(page,size),wrapper);
    }
}
